﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ServiceModel.DomainServices.Server;
using System.ServiceModel.DomainServices.Hosting;
using System.Data.Objects;

namespace Imtihan.Web
{
    public partial class ImtihanDomainService
    {
        /// <summary>
        /// Get a user by passing a user's id
        /// </summary>
        /// <param name="id">user's id</param>
        /// <returns>user object</returns>
        [Invoke]
        public User GetUserByIdAndPassword(string id, string password)
        {
            return this.ObjectContext.Users.Include("UserRoles.Role").Where(u => u.Id.ToLower() == id.ToLower() && u.Password == password).FirstOrDefault();
        }

        /// <summary>
        /// Get user roles by user's id.
        /// </summary>
        /// <param name="userId">User's id.</param>
        /// <returns>List of user's role.</returns>
        [Invoke]
        public List<Role> GetUserRolesByUserId(string userId)
        {
            //return this.ObjectContext.UserRoles.Where(ur => ur.UserId.ToLower() == userId.ToLower()).ToList();
            User user = this.ObjectContext.Users.Include("UserRoles.Role").Where(u => u.Id.ToLower() == userId.ToLower()).FirstOrDefault();

            List<Role> roles = new List<Role>();
            foreach (UserRole userRole in user.UserRoles)
            {
                roles.Add(userRole.Role);
            }

            return roles;
        }

    }
}